package com.ct.web.common.interceptors;

import org.springframework.web.context.request.WebRequestInterceptor;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.ui.ModelMap;
import org.springframework.beans.propertyeditors.LocaleEditor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 25.10.2009
 * Time: 0:27:50
 * To change this template use File | Settings | File Templates.
 */
public class LocaleInterceptor extends HandlerInterceptorAdapter {
    public static final String LOCALE_ATTR = "LOCALE_ATTR";

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(httpServletRequest);
        Locale ruLocale = new Locale("ru", "RU");
//        LocaleEditor localeEditor = new LocaleEditor();
//        localeEditor.setAsText("ru");

        localeResolver.setLocale(httpServletRequest, httpServletResponse, ruLocale);
        httpServletRequest.setAttribute(LOCALE_ATTR, ruLocale.getLanguage());
        return true;
    }
}

